#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHeaderView>
#include <QActionGroup>
#include <QCheckBox>
#include <QButtonGroup>

MainWindow::MainWindow(BasicWindow *parent)
    : BasicWindow(parent)
    , ui(new Ui::MainWindow), listWidget(nullptr)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    initLayout();
}

void MainWindow::initLayout()
{
    // 自定义工具栏和菜单栏位置
    mainLayout = new QVBoxLayout(this);

    initTitleBar(this, ":/action/images/companyLogo.png",
                "工程管理器", ButtonStyle::MIN_MAX_CLOSE_BUTTON, QColor(59, 87, 110));


    mainLayout->addWidget(titleBar);
    ui->menubar->setStyleSheet("QMenuBar { background: transparent; }");
    mainLayout->addWidget(ui->menubar);
    mainLayout->addWidget(ui->toolBar);

    tableWidget = new QTableWidget(this);
    initTableWidget();
    mainLayout->addWidget(tableWidget);

    mainLayout->setAlignment(Qt::AlignTop);
    mainLayout->setSpacing(0);
    mainLayout->setMargin(1);
    centralWidget()->setLayout(mainLayout);

    // 将视图action添加到一组
    QActionGroup *actionGroup = new QActionGroup(this);
    actionGroup->addAction(ui->actionSuperBigIcon);
    actionGroup->addAction(ui->actionBigIcon);
    actionGroup->addAction(ui->actionSmallIcon);
    actionGroup->addAction(ui->actionList);
    actionGroup->addAction(ui->actionDetail);
}

void MainWindow::initTableWidget()
{
    tableWidget->setRowCount(20);
    tableWidget->setColumnCount(8);

    tableWidget->setHorizontalHeaderLabels({"", "工程ID", "工程名称", "路径",
                                            "分辨率", "版本", "修改日期", "描述"});

    for (int i = 0; i < tableWidget->columnCount()-1; i++) {
        if (i == 0) {
            tableWidget->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Fixed);
            tableWidget->setColumnWidth(0, 30);
        } else {
            tableWidget->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Interactive);
        }
    }
    tableWidget->horizontalHeader()->setStretchLastSection(true);


    QStringList list = {"", "{B70356A5-56DC-4FE4-AE12-EB5FB52F530B)", "Hl_Test",
                        "C:/Program Files(X86)/HLXd", "1920*1080", "23.1.000",
                        "2023-12-13 15:21:45", "暂无"};

    for (int i = 0; i < tableWidget->rowCount(); i++) {
        for (int j = 0; j < list.size(); j++) {
            QTableWidgetItem *item = new QTableWidgetItem(list[j]);
            item->setTextAlignment(Qt::AlignCenter);
            tableWidget->setItem(i, j, item);
        }
    }

    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

    // 设置选中行的选中色
    QPalette palette = tableWidget->palette();
    palette.setColor(QPalette::Highlight, QColor(211, 211, 211));
    tableWidget->setPalette(palette);

    // 设置竖向表头不可见
    tableWidget->verticalHeader()->setVisible(false);

    // 设置单选
    QButtonGroup *group = new QButtonGroup(this);
    for (int row = 0; row < tableWidget->rowCount(); row++) {
        // 设置第一列为checkbox
        QWidget *checkBoxWidget = new QWidget(this);
        QHBoxLayout* boxLayout = new QHBoxLayout(checkBoxWidget);   // 需要居中显示, 添加布局
        QCheckBox* checkBox = new QCheckBox;

        // 设置大小, 隐藏边框
        checkBox->setFixedSize(20, 20);
        checkBoxWidget->setFixedSize(30, 30);
        group->addButton(checkBox);
        checkBox->setStyleSheet("QCheckBox::indicator { border: none;"
                                "                       width: 20px;"
                                "                       height: 20px; }"
                                "QCheckBox::indicator::checked { image: url(:/action/images/tick.png);}");
        boxLayout->addWidget(checkBox);
        boxLayout->setAlignment(Qt::AlignCenter);
        boxLayout->setMargin(0);
        tableWidget->setCellWidget(row, 0, checkBoxWidget);
    }

    // 只能选中单个, 选择后取消上一个勾选
    connect(tableWidget, &QTableWidget::currentItemChanged,
            this, &MainWindow::setChecked);
}

void MainWindow::setChecked(QTableWidgetItem *current, QTableWidgetItem *previous)
{
    // 选中当前选择的勾选框
    int row = current->row();
    QCheckBox* checkBox = qobject_cast<QCheckBox*>(
                tableWidget->cellWidget(row, 0)->layout()->itemAt(0)->widget());
    checkBox->setChecked(true);

    // 如果有上一个选择的, 移除上一个的勾选
    if (previous != nullptr) {
        int rowp = previous->row();
        QCheckBox* checkBox = qobject_cast<QCheckBox*>(
                    tableWidget->cellWidget(rowp, 0)->layout()->itemAt(0)->widget());
        checkBox->setChecked(false);
    }
}

void MainWindow::changeActionStatus()
{
    auto action = qobject_cast<QAction*>(sender());
    action->setChecked(false);
}

void MainWindow::on_actionNew_triggered()
{
    newProject = new NewProject(this);
    newProject->setWindowModality(Qt::WindowModal);
    newProject->show();
}

void MainWindow::on_actionRecover_triggered()
{
    projectRecover = new ProjectRecover(this);
    projectRecover->setWindowModality(Qt::ApplicationModal);
    projectRecover->show();
}

void MainWindow::on_actionPack_triggered()
{
    projectPack = new ProjectPack(this);
    projectPack->setWindowModality(Qt::ApplicationModal);
    projectPack->show();
}



void MainWindow::on_actionList_triggered(bool checked)
{
    if (checked) {
        if (listWidget != nullptr) {
            listWidget->setVisible(false);
        }
        tableWidget->setVisible(true);

        tableWidget->setRowCount(20);
        tableWidget->setColumnCount(5);
        tableWidget->setHorizontalHeaderLabels({"", "工程ID", "工程名称", "路径", "描述"});
        tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        for (int i = 0; i < tableWidget->columnCount()-1; i++) {
            tableWidget->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Interactive);
        }
        tableWidget->horizontalHeader()->setStretchLastSection(true);

        QStringList list = {"", "{B70356A5-56DC-4FE4-AE12-EB5FB52F530B)", "Hl_Test",
                            "C:/Program Files(X86)/HLXd", "暂无"};

        for (int i = 0; i < tableWidget->rowCount(); i++) {
            for (int j = 0; j < list.size(); j++) {
                QTableWidgetItem *item = new QTableWidgetItem(list[j]);
                tableWidget->setItem(i, j, item);
            }
        }
    }
}

void MainWindow::on_actionExit_triggered()
{
    close();
}

void MainWindow::on_actionDetail_triggered(bool checked)
{
    if (checked) {
        if (listWidget != nullptr) {
            listWidget->setVisible(false);
        }
        tableWidget->setVisible(true);

        initTableWidget();
    }
}

void MainWindow::on_actionSmallIcon_triggered(bool checked)
{
    if (checked) {
        tableWidget->setVisible(false);
        if (listWidget == nullptr) {
            listWidget = new QListWidget(this);
        }

        listWidget->clear();

        listWidget->setViewMode(QListView::IconMode);
        listWidget->setFlow(QListView::LeftToRight);
        listWidget->setResizeMode(QListView::Adjust);   // 自适应调整
        listWidget->setWrapping(true);      // 允许换行显示
        listWidget->setSpacing(22);
        listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
        // 设置样式表，确保选中时有视觉反馈
        listWidget->setStyleSheet(
            "QListWidget::item:selected {"
            "    border: 2px solid rgb(88, 131, 187);"       // 设置选中时的边框
            "}"
            "QListWidget::item {"
            "    border: 1px solid rgb(211, 211, 211);"
            "    border-radius: 3px;"
            "}");


        // 取得表格视图下的数据条数, 并使用其工程名字和日期进行初始化
        QString name;
        QString date;
        for (int i = 0; i < tableWidget->rowCount(); i++) {
            smallIconItem = new SmallIconItem(this);
            // 设置小图模式下的图片和文本
            smallIconItem->setImage();

            // 获取文本, 演示效果, 实际数据应该从数据库中读取
            name = "Hl_Test";
            date = "2023-12-13";

            smallIconItem->setText(name, date);

            // 添加item
            QListWidgetItem *item = new QListWidgetItem(listWidget);
            //listWidget->setGridSize(QSize(251, 195));
            item->setSizeHint(QSize(225, 167));
            listWidget->addItem(item);
            listWidget->setItemWidget(item, smallIconItem);
        }

        mainLayout->addWidget(listWidget);
        listWidget->setDragEnabled(false);
        listWidget->setVisible(true);
    }
}

void MainWindow::on_actionBigIcon_triggered(bool checked)
{
    if (checked) {
        tableWidget->setVisible(false);
        if (listWidget == nullptr) {
            listWidget = new QListWidget(this);
        }

        listWidget->clear();

        listWidget->setViewMode(QListView::IconMode);
        listWidget->setFlow(QListView::LeftToRight);
        listWidget->setResizeMode(QListView::Adjust);   // 自适应调整
        listWidget->setWrapping(true);      // 允许换行显示
        listWidget->setSpacing(19);
        listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
        // 设置样式表，确保选中时有视觉反馈
        listWidget->setStyleSheet(
            "QListWidget::item:selected {"
            "    border: 2px solid rgb(88, 131, 187);"       // 设置选中时的边框
            "}"
            "QListWidget::item {"
            "    border: 1px solid gray;"
            "    border-radius: 3px;"
            "}");


        // 取得表格视图下的数据条数, 并使用其工程名字和日期进行初始化
        QString name;
        QString date;
        for (int i = 0; i < tableWidget->rowCount(); i++) {
            smallIconItem = new SmallIconItem(this);
            // 重设item大小
            smallIconItem->setFixedSize(290, 200);
            smallIconItem->setItemSize(QSize(288, 167));
            // 设置小图模式下的图片和文本
            smallIconItem->setImage();

            // 获取文本, 演示效果, 实际数据应该从数据库中读取
            name = "Hl_Test";
            date = "2023-12-13";

            smallIconItem->setText(name, date);

            // 添加item
            QListWidgetItem *item = new QListWidgetItem(listWidget);
            // listWidget->setGridSize(QSize(314, 225));
            item->setSizeHint(QSize(290, 200));
            listWidget->addItem(item);
            listWidget->setItemWidget(item, smallIconItem);
        }

        mainLayout->addWidget(listWidget);
        listWidget->setDragEnabled(false);
        listWidget->setVisible(true);
    }
}

void MainWindow::on_actionSuperBigIcon_triggered(bool checked)
{
    if (checked) {
        tableWidget->setVisible(false);
        if (listWidget == nullptr) {
            listWidget = new QListWidget(this);
        }
        listWidget->clear();

        listWidget->setViewMode(QListView::IconMode);
        listWidget->setFlow(QListView::LeftToRight);
        listWidget->setResizeMode(QListView::Adjust);   // 自适应调整
        listWidget->setWrapping(true);      // 允许换行显示
        listWidget->setSpacing(28);
        listWidget->setSelectionMode(QAbstractItemView::SingleSelection);

        // 设置样式表，确保选中时有视觉反馈
        listWidget->setStyleSheet(
            "QListWidget::item:selected {"
            "    border: 2px solid rgb(88, 131, 187);"       // 设置选中时的边框
            "}"
            "QListWidget::item {"
            "    border: 1px solid rgb(211, 211, 211);"
            "    border-radius: 3px;"
            "}");


        // 取得表格视图下的数据条数, 并使用其工程名字和日期进行初始化
        QString nameLib;
        QString DPI;
        QString description;
        for (int i = 0; i < tableWidget->rowCount(); i++) {
            superBigIconItem = new SuperBigIconItem(this);
            // 重设item大小
            superBigIconItem->setFixedSize(380, 295);
            // 设置小图模式下的图片和文本
            superBigIconItem->setImage();

            // 获取文本, 演示效果, 实际数据应该从数据库中读取
            nameLib = "云上实验室";
            DPI = "1920*1080";
            description = "用于数据监视、设备控制管理、产线标准化生产等一系列产品自动化生产工艺";

            superBigIconItem->setText(nameLib, DPI, description);

            // 添加item
            QListWidgetItem *item = new QListWidgetItem(listWidget);
            // listWidget->setGridSize(QSize(314, 225));
            item->setSizeHint(QSize(382, 295));
            listWidget->addItem(item);
            listWidget->setItemWidget(item, superBigIconItem);
        }

        mainLayout->addWidget(listWidget);
        listWidget->setDragEnabled(false);
        listWidget->setVisible(true);
    }
}

void MainWindow::on_actionNewProject_triggered()
{
    newProject = new NewProject(this);
    newProject->setWindowModality(Qt::WindowModal);
    newProject->show();
}

void MainWindow::on_actionQuit_triggered()
{
    close();
}
